﻿using UnityEngine;
using System.Collections;
using System;

public  class ObjectBase : IObjectProperty
{
    public int _ID = 0;

    public int _Camera_layer = 0;

    public ObjectBase(string name, int layer)
    {
        _EditorName = name;

        _Camera_layer = layer;

        _Object = new GameObject(name);

        _Object.layer = layer;

        _ID = _Object.GetInstanceID();

        _SR = _Object.AddComponent<SpriteRenderer>();

    }

    public virtual void Load()
    {
        _bc2d = _Object.AddComponent<BoxCollider2D>();

        _dc = _Object.AddComponent<DrawComponent>();

    }

    /// <summary>
    /// 设置渲染顺序
    /// </summary>
    /// <param name="sorting"></param>
    public virtual void SetSortingOrder(int sorting)
    {
        _SR.sortingOrder = sorting;
    }

    /// <summary>
    /// 设置可见性
    /// </summary>
    /// <param name="visible"></param>
    public virtual void SetVisible(bool visible)
    {
        _SR.enabled = visible;
    }

    public virtual void SetColor(Color color)
    {
        _SR.color = color;
    }

    /// <summary>
    /// 设置排序策略
    /// </summary>
    /// <param name="order"></param>
    /// <param name="ics"></param>
    public virtual void SetCollate(nLayerOrder order, ICollateStrategy ics)
    {

    }

    public virtual bool Save()
    {
        return false;
    }

    public virtual void Unload()
    {
        _SR = null;

        GameObject.Destroy(_Object);
    }

    public virtual int Effect()
    {
        return -1;
    }

    //  显示的名字
    public string _EditorName;

    //  对象
    public GameObject _Object;

    //  图元
    protected SpriteRenderer _SR;

    //  世界位置
    public Vector3 _WorldPos = Vector3.zero;

    //
    public BoxCollider2D _bc2d = null;

    //
    public DrawComponent _dc = null;

    //
    public RhombusGridComponent _rgc = null;
    
    //
    public int _GroudOrder = -1;

    public virtual bool isLayerCollider
    {
        get
        {
            return true;
        }
    }


    public bool isDrag
    {
        get
        {
            return true;
        }
    }

    protected bool _isSelect = true;

    public virtual bool isSelect
    {
        get
        {
            return _isSelect;
        }
        set
        {
            _isSelect = value;
        }
    }

    public virtual bool isHitTransparent
    {
        get
        {
            return false;
        }
    }

    public virtual void Move(Vector2 delta)
    {
        Vector3 pos = _Object.transform.position;

        pos.x += delta.x / ZoneCfg.PixelPerUnit;
        pos.y += delta.y / ZoneCfg.PixelPerUnit;

        pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0;

        _Object.transform.position = pos;
        _WorldPos = pos;

        _dc.RefreshBorder();
        _dc.RefreshRhombus();

        _dc.RefreshDescription("World Pos: " + _Object.transform.position.ToString());

    }

    public virtual bool IsSelect(GameObject obj)
    {
        return false;
    }

    public virtual ObjectBase Selected(GameObject obj)
    {
        return null;
    }

    public virtual void Selected(bool flag)
    {
        _WorldPos = _Object.transform.position;

        _dc.RefreshBorder();
        _dc.RefreshRhombus();
    }

    public virtual void Gizmos(bool flag)
    {
        _dc.EnableGizmos(flag);
    }

    public virtual void Block(bool flag)
    {
        _rgc.EnableGizmos(flag);
    }

}
